package com.jinlejia.erp.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.lab.user.LabUserInfo;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class SharePTool {

    /**
     * 保存定位
     */
    public static void saveLocation(Context context, double longitude, double latitude, String city, String country) {
        SharedPreferences shared = context.getSharedPreferences("jinlejia", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = shared.edit();
        edit.putString("city", city);
        edit.putString("country", country);
        edit.putString("lng", String.valueOf(longitude));
        edit.putString("lat", String.valueOf(latitude));
        edit.commit();
    }

    /**
     * 获取定位区县
     */
    public static String getCountry(Context context) {
        SharedPreferences shared = context.getSharedPreferences("jinlejia", Context.MODE_PRIVATE);
        return shared.getString("country", "");
    }

    /**
     * 保存城市
     */
    public static void saveCity(Context context, String city) {
        SharedPreferences shared = context.getSharedPreferences("jinlejia", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = shared.edit();
        edit.putString("city", city);
        edit.commit();
    }

    /**
     * 获取定位城市
     */
    public static String getCity(Context context) {
        SharedPreferences shared = context.getSharedPreferences("jinlejia", Context.MODE_PRIVATE);
        return shared.getString("city", "天津市");
    }

    /**
     * 获取经度
     */
    public static double getLongitude(Context context) {
        SharedPreferences shared = context.getSharedPreferences("jinlejia", Context.MODE_PRIVATE);
        return Double.parseDouble(shared.getString("lng", "117.216548"));
    }

    /**
     * 获取纬度
     */
    public static double getLatitude(Context context) {
        SharedPreferences shared = context.getSharedPreferences("jinlejia", Context.MODE_PRIVATE);
        return Double.parseDouble(shared.getString("lat", "39.137438"));
    }

    /**
     * 历史访问城市
     */
    public static void saveHistoryCity(Context context, String city) {
        SharedPreferences shared = context.getSharedPreferences("jinlejia", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = shared.edit();
        com.alibaba.fastjson.JSONArray jsonArray = JSON.parseArray(shared.getString("hostory_city3", "[]"));
        //1. 已添加3个，删除第一个
        if (jsonArray.size() == 3 && !jsonArray.contains(city)) {
            jsonArray.remove(0);
        }
        //2. 已经添加过当前城市，删除该城市
        if (jsonArray.contains(city)) {
            jsonArray.remove(city);
        }
        //3.直接添加
        jsonArray.add(city);
        //持久化存储数据
        edit.putString("hostory_city3", jsonArray.toString());
        edit.commit();
    }

    /**
     * 历史访问城市
     */
    public static List<String> getHistoryCity(Context context) {

        SharedPreferences shared = context.getSharedPreferences("jinlejia", Context.MODE_PRIVATE);
        List<String> list = JSON.parseArray(shared.getString("hostory_city3", ""), String.class);
        if (null == list)
            list = new ArrayList<>();
        return list;
    }

    public static void deleteUserInfoBean(Context context) {
        SharedPreferences shared = context.getSharedPreferences("JLJ_UserInfoBean", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = shared.edit();
        edit.remove("cookie");
        edit.remove("pin");
        edit.remove("userName");
        edit.remove("telNum");
        edit.remove("imageUrl");
        edit.remove("email");
        edit.remove("deptName");
        edit.remove("titleLevel");
        edit.remove("shareStoreName");
        edit.apply();
    }

    public static UserInfoBean getUserInfoBean(Context context) {
        UserInfoBean bean = new UserInfoBean();
        SharedPreferences shared = context.getSharedPreferences("JLJ_UserInfoBean", Context.MODE_PRIVATE);
        bean.cookie = shared.getString("cookie", "");
        bean.pin = shared.getString("pin", "");
        bean.userName = shared.getString("userName", "");
        bean.telNum = shared.getString("telNum", "");
        bean.imageUrl = shared.getString("imageUrl", "");
        bean.email = shared.getString("email", "");
        bean.deptName = shared.getString("deptName", "");
        bean.titleLevel = shared.getString("titleLevel", "");
        bean.shareStoreName = shared.getString("shareStoreName", "");
        return bean;
    }

    public static UserInfoBean saveUserInfoBean(Context context, UserInfoBean bean) {
        SharedPreferences shared = context.getSharedPreferences("JLJ_UserInfoBean", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shared.edit();
        String cookie = shared.getString("cookie", null);
        if (TextUtils.isEmpty(cookie) || !cookie.equals(bean.getCookie())) {
            editor.putString("cookie", bean.cookie);
        }
        String pin = shared.getString("pin", null);
        if (TextUtils.isEmpty(pin) || !pin.equals(bean.getPin())) {
            editor.putString("pin", bean.pin);
        }
        String userName = shared.getString("userName", null);
        if (TextUtils.isEmpty(userName) || !userName.equals(bean.getUserName())) {
            editor.putString("userName", bean.userName);
        }
        String telNum = shared.getString("telNum", null);
        if (TextUtils.isEmpty(telNum) || !telNum.equals(bean.getTelNum())) {
            editor.putString("telNum", bean.telNum);
        }
        String imageUrl = shared.getString("imageUrl", null);
        if (TextUtils.isEmpty(imageUrl) || !imageUrl.equals(bean.getImageUrl())) {
            editor.putString("imageUrl", bean.imageUrl);
        }
        String email = shared.getString("email", null);
        if (TextUtils.isEmpty(email) || !email.equals(bean.getEmail())) {
            editor.putString("email", bean.email);
        }
        String deptName = shared.getString("deptName", null);
        if (TextUtils.isEmpty(deptName) || !deptName.equals(bean.getDeptName())) {
            editor.putString("deptName", bean.deptName);
        }
        String titleLevel = shared.getString("titleLevel", null);
        if (TextUtils.isEmpty(titleLevel) || !titleLevel.equals(bean.getTitleLevel())) {
            editor.putString("titleLevel", bean.titleLevel);
        }
        String shareStoreName = shared.getString("shareStoreName", null);
        if (TextUtils.isEmpty(shareStoreName) || !shareStoreName.equals(bean.getShareStoreName())) {
            editor.putString("shareStoreName", bean.shareStoreName);
        }
        editor.apply();
        return bean;
    }

    public final static class UserInfoBean implements LabUserInfo {
        private String pin;
        private String cookie;
        private String userName;
        private String telNum;
        private String imageUrl;
        private String email;
        private String deptName;
        private String titleLevel;
        private String shareStoreName;


        public String getShareStoreName() {
            return shareStoreName;
        }

        public void setShareStoreName(String shareStoreName) {
            this.shareStoreName = shareStoreName;
        }

        public String getCookie() {
            return cookie;
        }

        public void setCookie(String cookie) {
            this.cookie = cookie;
        }

        public void setPin(String pin) {
            this.pin = pin;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public void setTelNum(String telNum) {
            this.telNum = telNum;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public void setDeptName(String deptName) {
            this.deptName = deptName;
        }

        public void setTitleLevel(String titleLevel) {
            this.titleLevel = titleLevel;
        }

        public String getPin() {
            return pin;
        }

        public String getUserName() {
            return userName;
        }

        public String getTelNum() {
            return telNum;
        }

        public String getImageUrl() {
            return imageUrl;
        }

        public String getEmail() {
            return email;
        }

        public String getDeptName() {
            return deptName;
        }

        public String getTitleLevel() {
            return titleLevel;
        }
    }
}
